package com.rtx.edi.platform.application.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.edi.model.application.dto.PlAppEnSeLiDTO;
import com.rtx.edi.model.application.po.PlApplicationEnvironment;
import com.rtx.edi.platform.application.mapper.PlApplicationEnvironmentMapper;
import com.rtx.edi.platform.application.service.PlApplicationEnvironmentService;

/**
 * <p>
 * 应用环境信息表  服务实现类
 * </p>
 *
 * @author cafebabe
 * @since 2023-03-06
 */
@Service
public class PlApplicationEnvironmentServiceImpl extends ServiceImpl<PlApplicationEnvironmentMapper, PlApplicationEnvironment> implements PlApplicationEnvironmentService {

	public void selectPlApplicationEnvironmentList(Page<PlApplicationEnvironment> pageParam, PlApplicationEnvironment plApplicationEnvironment) {
	       QueryWrapper<PlApplicationEnvironment> query = new QueryWrapper<>();
	        query.orderByAsc(true,Arrays.asList("company_id,environment_no".split(",")));
	        query.eq("is_deleted", "0");
	        if(plApplicationEnvironment != null){ 
	            if(StringUtils.hasText(plApplicationEnvironment.getEnvironmentName())){
	                query.like("environment_name",plApplicationEnvironment.getEnvironmentName());
	            }
	            if(StringUtils.hasText(plApplicationEnvironment.getEnvironmentNo())){
	            	query.like("environment_no",plApplicationEnvironment.getEnvironmentNo());
	            }
	            if(plApplicationEnvironment.getEnvironmentStatus() != null){
	            	query.eq("environment_status", plApplicationEnvironment.getEnvironmentStatus());
	            }
	        }
	        baseMapper.selectPage(pageParam,query);
	}

	public void checkAndaddPlApplicationEnvironment(PlApplicationEnvironment entity) {
		  QueryWrapper<PlApplicationEnvironment> query = new QueryWrapper<>();
		  query.eq("environmentName", entity.getEnvironmentStatus());
		  List<PlApplicationEnvironment> list =   Optional.ofNullable(baseMapper.selectList(query)).orElse(new ArrayList<PlApplicationEnvironment>());
		  if(list.size() < 1) {
			  entity.setEnvironmentStatus(50302);
			  baseMapper.insert(entity);
		  }
	}
	
	public void delPlApplicationEnvironment(Long id) {
		PlApplicationEnvironment po = new PlApplicationEnvironment();
		po.setId(id);
		po.setIsDeleted(1);
		baseMapper.updateById(po);
	}

	@Transactional
	public void delPlApplicationEnvironment(List<PlAppEnSeLiDTO> list) {
		for(PlAppEnSeLiDTO dto:list) {
			PlApplicationEnvironment po = new PlApplicationEnvironment();
			po.setId(dto.getId());
			po.setIsDeleted(1);
			baseMapper.updateById(po);
		}
	}
	
	
	
}
